﻿@using Microsoft.Extensions.Localization
@using Volo.Abp.FeatureManagement.Localization
@using Volo.Abp.Validation.StringValues
@inject IStringLocalizer<AbpFeatureManagementResource> L

<Modal @ref="_modal">
    <ModalBackdrop />
    <ModalContent Size="ModalSize.Large" IsCentered="true">
        <ModalHeader>
            <ModalTitle>@L["Features"]</ModalTitle>
            <CloseButton Clicked="CloseModal" />
        </ModalHeader>
        <ModalBody MaxHeight="50">
            @if (_groups == null)
            {
                <span>@L["NoFeatureFoundMessage"]</span>
            }
            else
            {
                <Tabs TabPosition="TabPosition.Left" Pills="true" SelectedTab="@GetNormalizedGroupName(_groups.First().Name)">
                    <Items>
                        @foreach (var group in _groups)
                        {
                            <Tab Name="@GetNormalizedGroupName(group.Name)">
                                <span>@group.DisplayName</span>
                            </Tab>
                        }
                    </Items>
                    <Content>
                        @foreach (var group in _groups)
                        {
                            <TabPanel Name="@GetNormalizedGroupName(group.Name)">
                                <h4>@group.DisplayName</h4>
                                
                                @foreach (var feature in group.Features)
                                {
                                    var disabled = IsDisabled(feature.Provider.Name);
                                    
                                    if (feature.ValueType is FreeTextStringValueType)
                                    {
                                        <Field>
                                            <FieldLabel>@feature.DisplayName</FieldLabel>
                                            <TextEdit Disabled="@disabled" @bind-text="@feature.Value" />
                                            @if (feature.Description != null)
                                            {
                                                <span>@feature.Description</span>
                                            }
                                        </Field>
                                    }

                                    if (feature.ValueType is SelectionStringValueType)
                                    {
                                        var items = ((SelectionStringValueType) feature.ValueType).ItemSource.Items;
                                        
                                        <Field>
                                            <FieldLabel>@feature.DisplayName</FieldLabel>
                                            <Select TValue="string" SelectedValue="feature.Value">
                                                @foreach (var item in items)
                                                {
                                                    <SelectItem Value="@item.Value">@item.DisplayText</SelectItem>
                                                }
                                            </Select>
                                        </Field>
                                    }

                                    if (feature.ValueType is ToggleStringValueType)
                                    {
                                        <Field>
                                            <Check TValue="bool" @bind-checked="@ToggleValues[feature.Name]">@feature.DisplayName</Check>
                                        </Field>
                                    }
                                }
                                
                            </TabPanel>
                        }
                    </Content>
                </Tabs>
            }
        </ModalBody>
        <ModalFooter>
            <Button Color="Color.Secondary" Clicked="CloseModal">@L["Cancel"]</Button>
            <Button Color="Color.Primary" Clicked="SaveAsync">@L["Save"]</Button>
        </ModalFooter>
    </ModalContent>
</Modal>